package com.nine.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nine.dao.AccountMapper;
import com.nine.dao.BillsMapper;
import com.nine.dao.UsersMapper;
import com.nine.entity.Account;
import com.nine.entity.Bills;
import com.nine.entity.Users;
import com.nine.service.BillsService;

/**
 * @author hubing
 *
 */
@Transactional
@Service
public class BillsServiceImpl implements BillsService {
	@Autowired
	BillsMapper billsMapper;
	@Autowired
	UsersMapper usersMapper;
	@Autowired
	AccountMapper accountMapper;

	// 通过登录用户主键（ID），查询登录用户银行的余额
	public float selectAccountMoneyByUserId(int id) {
		Users user = usersMapper.selectByPrimaryKey(id);// 通过用户主键ID查到用户信息
		Account account = accountMapper.selectByPrimaryKey(Integer.parseInt(user.getAccountid()));// 通过用户外键（accountid），查询银行账户信息
		return account.getMoney();
	}

	// 根据用户表（username）查询用户是否存在，且非注册会员。
	@Override
	public int validationMemberByUsername(String username) {
		return billsMapper.userIsExistenceByUsername(username);
	}

	// 验证用户银行账户余额是否足够转账。
	@Override
	public boolean validationMoney(int id, float money) {
		return billsMapper.selectAccountMoney(id) >= money;
	}

	// 验证登录用户二级支付密码是否正确
	@Override
	public boolean verificationPassword(int id, int paycode) {
		String accountid = billsMapper.selectAccountidByUserId(id);
		return paycode == billsMapper.selectAccountPaycodeByAccountid(Integer.parseInt(accountid));
	}

	// 减少登录用户银行余额，增加目标用户余额。添加两条交易订单记录到数据库
	@Override
	public void transferOperation(int uid, String rid, float money) {
		// 减少登录用户的金额——start
		Account acc_uid = new Account();
		acc_uid.setAccountid(Integer.parseInt(billsMapper.selectAccountidByUserId(uid)));
		acc_uid.setMoney(money);
		billsMapper.reduceMoney(acc_uid);
		// 减少登录用户的金额——end

		// 增加目标用户的金额——start
		Account acc_rid = new Account();
		Users ridUsers = billsMapper.selectUserByUsername(rid);
		acc_rid.setAccountid(Integer.parseInt(ridUsers.getAccountid()));
		acc_rid.setMoney(-money);
		billsMapper.reduceMoney(acc_rid);
		// 增加目标用户的金额——end

		Date date = new Date();// 发起时间
		String bidU = getBidUUID();// 获取转账支出订单
		String bidR = getRidUUID(bidU);// 获取转账收入订单

		Users user1 = usersMapper.selectByPrimaryKey(uid);// 登录用户
		Users user2 = usersMapper.selectByPrimaryKey(ridUsers.getId());// 目标用户

		// 添加转账支出订单——start
		Bills billsU = new Bills();
		billsU.setBid(bidU);
		billsU.setUid(user1.getUsername());
		billsU.setRid(user2.getUsername());
		billsU.setMoneyout(money);
		billsU.setDate(date);
		billsU.setKeywords("转账支出");
		int moneynowU = (int) (billsMapper.selectAccountMoney(user1.getId()) + money);
		billsU.setMoneynow(moneynowU - (int) money);
		billsU.setState(1);
		billsMapper.insertSelective(billsU);
		// 添加转账支出订单——end

		// 添加转账收入订单——start
		Bills billsR = new Bills();
		billsR.setBid(bidR);
		billsR.setUid(user1.getUsername());
		billsR.setRid(user2.getUsername());
		billsR.setMoneyin(money);
		billsR.setDate(date);
		billsR.setKeywords("转账收入");
		int moneynowR = (int) billsMapper.selectAccountMoney(user2.getId());
		billsR.setMoneynow(moneynowR + (int) money);
		billsR.setState(1);
		billsMapper.insertSelective(billsR);
		// 添加转账收入订单——end

	}

	// 获取20位订单号
	@Override
	public String getBidUUID() {
		String uuid;
		String bid;
		int temp = 0;
		do {
			uuid = UUID.randomUUID().toString();
			String uuidArray[] = uuid.split("-");
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
			bid = (sdf.format(new Date())) + "AA" + uuidArray[1];
			temp = accountMapper.selectUUID(bid);
		} while (temp == 1);

		return bid;
	}

	// 获取20位订单号,转账目标用户的订单号
	@Override
	public String getRidUUID(String bid) {
		String bidArray[] = bid.split("AA");
		return bidArray[0] + "BB" + bidArray[1];
	}

}
